package cn.lagou

import scala.collection.mutable
import scala.io.StdIn
import scala.util.Random

/**
 * 题目2 猜拳
 * 1. 选取对战角色
 * 2. 开始对战，用户出拳，与对手进行比较，提示胜负信息
 * 3. 猜拳结束算分，平局都加一分，获胜加二分，失败不加分
 * 4. 循环对战，当输入“n”时，终止对战，并显示对战结果
 * 5. 游戏结束后显示得分
 */
object FingerGuess {

  class Player() {
    var name = "游客"
    var score = 0
    var countMap: mutable.Map[String, Int] = mutable.Map(("win", 0), ("tie", 0), ("lose", 0))
  }

  class ComputerRole(var name: String) {
    var score = 0
    var countMap: mutable.Map[String, Int] = mutable.Map(("win", 0), ("tie", 0), ("lose", 0))

    //电脑随机出拳
    def showFist(): Int = {
      val seq = (1 to 3).map(x => x)
      val random = new Random()
      random.shuffle(seq).take(1).head
    }
  }

  def main(args: Array[String]): Unit = {
    println("*****************************")
    println("***********猜拳开始***********")
    println("*****************************")
    println("请选择对战角色：（1、刘备 2、关羽 3、张飞）")
    val roleMap = mutable.Map((1, "刘备"), (2, "关羽"), (3, "张飞"))
    val fingerMap = mutable.Map((1, "石头"), (2, "剪刀"), (3, "布"))
    var roleId = StdIn.readInt()
    while (roleId != 1 && roleId != 2 && roleId != 3) {
      println("没有这个选项，请重新输入:")
      roleId = StdIn.readInt()
    }
    val player = new Player
    val computerRole = new ComputerRole(roleMap(roleId))
    var flag = true //游戏是否继续
    var continue = "y" //是否继续
    var input = 3 //玩家出拳值
    var computerInput = 0 //电脑出拳值
    var gameCount = 0 //对局次数
    println(">>>>>>>>>>>>>>>>>>>>>>>游戏开始>>>>>>>>>>>>>>>>>>>>>>>>>>>")
    println("游戏介绍:1.石头输入[1] 2.剪刀输入[2] 3.布输入[3] 4.退出输入[n]")
    while (flag) {
      println("游客请出拳")
      try {
        input = StdIn.readInt()
        println(s"你出拳：${fingerMap(input)}")
      } catch {
        case _: Exception => input = 3
          println("出拳不符合规定，默认出布")
      }
      computerInput = computerRole.showFist()
      println(s"${computerRole.name}出拳：${fingerMap(computerInput)}")
      computeResult(input, computerInput, player, computerRole)
      gameCount += 1
      println("是否开始下一轮（y/n）")
      continue = StdIn.readLine()
      continue match {
        case "y" => flag = true
        case "n" => flag = false
        case _ =>
          println("没有这个选项，默认退出")
          flag = false
      }
    }
    //游戏结束，展示游戏结果
    showResult(player, computerRole, gameCount)
  }

  private def computeResult(playerInput: Int, computerInput: Int, player: Player, computerRole: ComputerRole): Unit = {
    var playerMap = player.countMap
    var computerMap = computerRole.countMap
    // 平局
    if (playerInput == computerInput) {
      player.score += 1
      computerRole.score += 1
      playerMap += ("tie" -> (playerMap("tie") + 1))
      computerMap += ("tie" -> (computerMap("tie") + 1))
      println("...平局")
    }
    // 玩家胜
    else if ((playerInput - computerInput) == -1 || (playerInput - computerInput) == 2) {
      player.score += 2
      playerMap += ("win" -> (playerMap("win") + 1))
      computerMap += ("lose" -> (computerMap("lose") + 1))
      println("^_^玩家胜")
    } else {
      computerRole.score += 2
      playerMap += ("lose" -> (playerMap("lose") + 1))
      computerMap += ("win" -> (computerMap("win") + 1))
      println(s"...${computerRole.name}胜")
    }
  }

  private def showResult(player: Player, computerRole: ComputerRole, gameCount: Int): Unit = {
    println("----------------------------------------------------------------")
    val playerMap = player.countMap
    val computerMap = computerRole.countMap
    println(s"${player.name}\tVS\t${computerRole.name}")
    println(s"对战次数${gameCount}次\n")
    println("姓名\t得分\t胜局\t和局\t负局")
    println(s"${player.name}\t${player.score}\t${playerMap("win")}\t${playerMap("tie")}\t${playerMap("lose")}")
    println(s"${computerRole.name}\t${computerRole.score}\t${computerMap("win")}\t${computerMap("tie")}\t${computerMap("lose")}")
  }


}
